#!/bin/bash

# Helper utility to read json values from a file
function readJson {
  UNAMESTR=`uname`
  if [[ "$UNAMESTR" == 'Linux' ]]; then
    SED_EXTENDED='-r'
  elif [[ "$UNAMESTR" == 'Darwin' ]]; then
    SED_EXTENDED='-E'
  fi; 

  VALUE=`grep -m 1 "\"${2}\"" ${1} | sed ${SED_EXTENDED} 's/^ *//;s/.*: *"//;s/",?//'`

  if [ ! "$VALUE" ]; then
    echo "Error: Cannot find \"${2}\" in ${1}" >&2;
    exit 1;
  else
    echo $VALUE ;
  fi;
}

USE_CHROME_STABLE=false;

if [[ $SOURCE_BRANCH == *"chrome-stable"* ]]; then
  USE_CHROME_STABLE=true;
fi

docker build \
  --build-arg "USE_CHROME_STABLE=$USE_CHROME_STABLE"\
  --label "browser=$(readJson version.json Browser)"\
  --label "protocolVersion=$(readJson version.json Protocol-Version)"\
  --label "v8Version=$(readJson version.json V8-Version)"\
  --label "webkitVersion=$(readJson version.json WebKit-Version)"\
  --label "debuggerVersion=$(readJson version.json Debugger-Version)"\
  --label "puppeteerVersion=$(readJson version.json Puppeteer-Version)"\
  -t $IMAGE_NAME .
